<?php
namespace App\YlyService;


use App\YlyService\typography;
class ylyPrint{

    public static function index($orderData, $printer,$status, $shopset)
    {
        $content = "";
        $version = typography::checkArray($printer["version"]);
        switch ($status) {
            case 32000:
                $content = self::createPayOrder($orderData, $version, $shopset);

                break;
            case 20020:
                $content = self::createCancelOrderStr($orderData, $version, $shopset);
                break;

        }
        return $content;
    }


    private static function createPayOrder($orderData, $version, $shopset)
    {
        $order = $orderData["resultList"][0];
        $shop = $order["produceStationName"];
        $goodsData = $order["product"];

        $orderStr = '';
        $casevoice = json_decode($shopset['voice'],true);
        if(!empty($casevoice)){
            if(in_array('1',$casevoice)){
                $orderStr = '<audio>'.'您有新的京东到家订单'.'</audio>';
            }
            if(in_array('2',$casevoice)){
                $dishesnames = '';
                foreach ($goodsData as $v) {

                    $dishesnames =  $v["skuName"] . $v["skuCostumeProperty"]  . $v["skuCount"] ."份" ;


                }
                $orderStr .= '<audio>'.$dishesnames.'</audio>';
            }
            if(in_array('3',$casevoice)){
                $orderStr .= '<audio>订单总价：'. ($order["orderReceivableFreight"] / 100).'元'.'</audio>';
            }
            if(in_array('4',$casevoice)){
                if($order["orderBuyerRemark"]){
                    $orderStr .= '<audio>订单备注：'.$order["orderBuyerRemark"].'</audio>';
                } else {
                    $orderStr .= '<audio>订单备注：无</audio>';
                }
            }
        }
        $orderStr .= typography::center(2,"**京东到家**");
        $orderStr .= typography::getStrRepeat($version, "-");
        /*if($order["produceStationName"] == 0){
            $payType = "--原订单--";
        }elseif ($order["produceStationName"] == 10){
            $payType = "--退款单--";
        }
        elseif ($order["produceStationName"] == 20){
            $payType = "--补货单--";
        }
        elseif ($order["produceStationName"] == 30){
            $payType = "--直赔商品--";
        }
        elseif ($order["produceStationName"] == 40){
            $payType = "--退货--";
        }*/
        $payType = ($order['orderPayType'] == 4) ? "--在线支付--" : "--货到付款--";
        $orderStr .= typography::center(2, $payType);
        /*if ($order["send_immediately"] == 2) {
            $orderStr .= typography::center(2, "[预约定单]");
        }*/
        $orderStr .= typography::center(2,$shop);
        $orderType = ($order["orderType"] == 10000) ? "--门店订单--" : "--其他来源--";
        $orderStr .= typography::FS($version, 1, "订单来源: " . $orderType);
        if (!empty($shopset["ordertime_size"])){
            $orderStr .= typography::FS($version, $shopset["ordertime_size"], "下单时间: " . $order["orderStartTime"]);
            $orderStr .= typography::FS($version, $shopset["ordertime_size"], "送达时间: " . $order["orderPreEndDeliveryTime"]);
        }else{
            $orderStr .= typography::FS($version, 1, "下单时间: " . $order["orderStartTime"]);
            $orderStr .= typography::FS($version, 1, "送达时间: " . $order["orderPreEndDeliveryTime"]);
        }


        $orderStr .= typography::FS($version, 1, "订单编号: " . $order["orderId"]);
        if (!empty($order["orderBuyerRemark"])) {
            if (!empty($shopset['remark_size'])) {
                $orderStr .= typography::FS($version, $shopset['remark_size'], "订单备注: " . $order["orderBuyerRemark"]);
            }else{
                $orderStr .= typography::FS($version, 2, "订单备注: " . $order["orderBuyerRemark"]);
            }
        }
        $orderStr .= typography::getStrRepeat($version, "*", "商品");
        if (!empty($shopset['goods_size'])) {
            $table = typography::table($shopset['goods_size']);
        }else{
            $table = typography::table(2);
        }
        foreach ($goodsData as $v) {

            $productsMsg = "<tr><td>" . $v["skuName"] . $v["skuCostumeProperty"] . "</td><td>" .'x'. $v["skuCount"] . "</td><td>" . $v["skuStorePrice"] . "</td></tr>";

            $orderStr .=   $table[0] . $productsMsg . $table[1];
        }
        $order ["orderFreightMoney"]= (empty($order["orderFreightMoney"])) ? '0' : $order["orderFreightMoney"] / 100;
        $order["orderBuyerPayableMoney"] = $order["orderBuyerPayableMoney"] / 100;
        $order["packagingMoney"] = $order["packagingMoney"] / 100;
        $order["orderReceivableFreight"] = $order["orderReceivableFreight"] / 100;
        $orderStr .= typography::getStrRepeat($version, "-");
        $orderStr .= typography::FS($version, 1, "配送费: ￥" . $order["orderReceivableFreight"]);
        $orderStr .= typography::FS($version, 1, "包装费: ￥" . $order["packagingMoney"]);
        $orderStr .= typography::FS($version, 1, "订单运费: ￥" . $order["orderFreightMoney"]);
        $orderStr .= typography::FS($version, 1, "订单实际总金额: ￥" . $order["orderBuyerPayableMoney"]);
        $orderStr .= typography::getStrRepeat($version, "*");
        /*if (!empty($order["invoice_title"])) {
            $orderStr .= typography::FS($version, 1, "发票抬头: " . $order["invoice_title"]);
        }
        if (!empty($order["taxer_id"])) {
            $orderStr .= typography::FS($version, 1, "纳税人识别号: " . $order["taxer_id"]);
        }*/

        //$phone = substr_replace($order["buyerMobile"],"-",7,0);
        //$phone = substr_replace($phone,"-",3,0);
        $falsephone = (empty($order['buyerMobile'])) ? $order['buyerTelephone'] : $order['buyerMobile'];
        $phone = $order['lastFourDigitsOfBuyerMobile'];
        if (!empty($shopset['recivephone_size'])) {
            $orderStr .= typography::FS($version, $shopset['recivephone_size'], "收货人电话: " . $falsephone);
            $orderStr .= typography::FS($version, $shopset['recivephone_size'], "收货人真实电话尾号: " . $phone);
        }else{
            $orderStr .= typography::FS($version, 1, "收货人电话: " . $falsephone);
            $orderStr .= typography::FS($version, 1, "收货人真实电话尾号: " . $phone);
        }
        if (!empty($shopset['reciveaddr_size'])) {
            $orderStr .= typography::FS($version, $shopset['reciveaddr_size'], "收货人地址: " . $order["buyerFullAddress"]);
        }else{
            $orderStr .= typography::FS($version, 1, "收货人地址: " . $order["buyerFullAddress"]);
        }
        //$gender = ($user["gender"] == 1) ? "先生" : "女士";
        $orderStr .= typography::FS($version, 1, "收货人: " . $order["buyerFullName"]);
        if (!empty($shopset['footer'])) {
            $orderStr .= typography::FS($version, 1, $shopset["footer"]);
        }
        $orderStr .= typography::center(2,"**" . " 完**");
        return $orderStr;
    }


    private static function getProductsMsgByGoodsDetail($goodsDetail)
    {
        $productAttr = "";
        $productFeatures = "";
        if (!empty($goodsDetail["product_attr"])) {
            foreach ($goodsDetail["product_attr"] as $val) {
                $productAttr .= $val["name"] . ",";
            }
        }
        if (!empty($goodsDetail["product_features"])) {
            foreach ($goodsDetail["product_features"] as $val) {
                $productFeatures .= $val["name"] . ",";
            }
        }
        if (!empty($productAttr)) {
            $productAttr = "[" . rtrim($productAttr, ",") . "]";
        }
        if (!empty($productFeatures)) {
            $productFeatures = "[" . rtrim($productFeatures, ",") . "]";
        }
        $goodsDetail["product_price"] = $goodsDetail["product_price"] / 100;
        return "<tr><td>" . $goodsDetail["product_name"] . $productAttr . $productFeatures . "</td><td>" . $goodsDetail["product_amount"] . "</td><td>" . $goodsDetail["product_price"] . "</td></tr>";
    }

    private static function getProductsMsgByGoodsDetails($goodsDetail)
    {
        return  $goodsDetail["product_name"] ." ".$goodsDetail["product_amount"]."份";
    }

    private static function createCancelOrderStr($orderData, $version, $shopset)
    {
        $orderStr = typography::FS($version, 2, "京东到家==在线取消单提醒");
        $orderStr .= typography::FS($version, 2, "订单ID：" . $orderData['order_id']);
        $orderStr .= typography::FS($version, 2, "取消详情：" . $orderData['reason']);
        return $orderStr;
    }

    private static function createPartRefundOrderStr($orderData, $version, $shopset)
    {
        $orderNo = $orderData['order_id'];                      //订单id
        $type = $orderData['type'];                             //部分退款类型
        $refundPrice = $orderData['refund_price'] / 100;        //退款总金额
        //部分退款原因reason  //部分退款额外原因addition_reason
        $reason = @$orderData['reason'] . @$orderData['addition_reason'];
        $refundProducts = $orderData['refund_products'];        //退款商品信息
        $size = 1;                                              //打印字体大小
        $orderStr = typography::FS(2, $size, '饿百零售==部分退款订单提醒');
        $orderStr .= typography::FS(2, $size, '订单ID：' . $orderNo);
        $table = typography::table(2);
        $orderStr .= $table[0];
        $orderStr .= '<tr><td>商品</td><td>数量</td><td>金额</td></tr>';
        foreach ($refundProducts as $val) {
            $val['total_refund'] = $val['total_refund'] / 100;
            $orderStr .= '<tr><td>' . $val['name'] . '</td><td>' . $val['number'] . '</td><td>' . $val['total_refund'] . '</td></tr>';
        }
        $orderStr .= typography::FS(2, $size, '退款总金额：' . $refundPrice);
        $orderStr .= typography::FS(2, $size, '退款原因：' . $reason);
        return $orderStr;
    }

    //暂时不需要打印用户在线申请取消订单
    private static function createUserCancelOrderStr($orderData, $version, $shopset)
    {

    }


}